#!/bin/bash
sudo rm -rf /usr/local/bin/dapr

GITHUB_ORG=dapr
GITHUB_REPO=cli
getLatestRelease() {
  local daprReleaseUrl="https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases"
  local latest_release=""

  if [ "$DAPR_HTTP_REQUEST_CLI" == "curl" ]; then
    latest_release=$(curl -s $daprReleaseUrl | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p')
  else
    latest_release=$(wget -q --header="Accept: application/json" -O - $daprReleaseUrl | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p')
  fi

  darpcli=$latest_release
}
getLatestRelease
echo "Dapr CLI latest version: ${darpcli}"
wget -qO - https://ghproxy.net/https://github.com/dapr/cli/releases/download/${darpcli}/dapr_linux_amd64.tar.gz | sudo tar xfz - -C /usr/local/bin/

# wget -q https://ghproxy.net/https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
